@file:OptIn(DelicateCoroutinesApi::class)

package com.common.app.utls.report

import com.common.app.data.bean.BaseCityModel
import com.common.app.data.bean.CloudDownDataModel
import com.common.app.data.bean.SearchHistoryModel
import com.common.app.db.*
import kotlinx.coroutines.*

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2022/8/21
 * <p>
 * 描述：上报事件的工具类
 * <p>
 * 修订历史：
 */
object ReportEventManager {

    private var TAG = ReportEventManager::class.java.simpleName
    private const val REPORT_SIZE = 20 //每次上报事件的个数

    private val scope = CoroutineScope(Dispatchers.IO)

    //用于保存待记录的事件
    private val mEventMap: HashMap<String, ReportEvent> by lazy { HashMap() }

    private val mReportEventDao: ReportEventDao by lazy { AppDataBase.getDb().getReportEventDao() }

    private val searchHistoryModelDao: SearchHistoryModelDao by lazy {
        AppDataBase.getDb().getSearchHistoryDao()
    }

    private val searchCityModelDao: SearchCityModelDao by lazy {
        AppDataBase.getDb().getSearchCityModelDao()
    }

    private val cloudDownDataDao: CloudDownDataDao by lazy {
        AppDataBase.getDb().getCloudDownDataDao()
    }

    fun getSearchHistoryModel(): MutableList<SearchHistoryModel> {
        return searchHistoryModelDao.findAll()
    }

    fun findByDescSize(): MutableList<SearchHistoryModel> {
        return searchHistoryModelDao.findByDescSize(20)
    }

    fun delAllSearchKey() {
        scope.launch {
            withContext(Dispatchers.IO) {
                searchHistoryModelDao.deleteAll()
            }
        }
    }

    @Synchronized
    fun saveSearchHistoryModel(mTemp: SearchHistoryModel) {

        scope.launch {
            withContext(Dispatchers.IO) {
                mTemp.searchKey?.let {
                    if (searchHistoryModelDao.findByKey(it).isNotEmpty()) {
                        searchHistoryModelDao.update(mTemp)
                    } else {
                        searchHistoryModelDao.insert(mTemp)
                    }
                }
            }
        }
    }

    fun findByCityModel(): MutableList<BaseCityModel> {
        return searchCityModelDao.findByDescSize(20)
    }

    @Synchronized
    fun saveCloudDownData(mList: List<CloudDownDataModel>?) {
        if (mList.isNullOrEmpty()) return
        scope.launch {
            withContext(Dispatchers.IO) {
                cloudDownDataDao.insert(mList)
            }
        }
    }

    fun getCloudDownDataById(mId: String?) : List<CloudDownDataModel>{
        if (mId.isNullOrEmpty()) return mutableListOf()
        return cloudDownDataDao.findById(mId)
    }


    fun getCloudDownDataByIdDescSize(mId: String?, mSize: Int =50) : List<CloudDownDataModel>{
        if (mId.isNullOrEmpty()) return mutableListOf()
        return cloudDownDataDao.findByIdDescSize(mId,mSize)
    }

    suspend fun delCloudDownData(mList: List<CloudDownDataModel>){
        if (mList.isNullOrEmpty()) return
        withContext(Dispatchers.IO) {
            cloudDownDataDao.deleteAll(mList)
        }
    }
}